//
//  PBMyInfoController.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/9.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBMyInfoController.h"
#import "PBMyInfoHeaderCell.h"
#import "PBMyListCell.h"
#import "PBLoginOutCell.h"
#import "PBChangeNickNameController.h"
#import "PBMyInfoModel.h"
#import "PBSuggestController.h"
#import "PBVerifyInfoController.h"
#import "PBAboutMeController.h"

static NSString * const kPBMyInfoHeaderCell = @"PBMyInfoHeaderCell";
static NSString * const kPBMyListCell = @"PBMyListCell";
static NSString * const kPBLoginOutCell = @"PBLoginOutCell";

@interface PBMyInfoController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) PBMyInfoModel *model;
@end

@implementation PBMyInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self configureData];
}
- (void)configureView {
    [self.tableView registerNib:[UINib nibWithNibName:kPBMyInfoHeaderCell bundle:nil] forCellReuseIdentifier:kPBMyInfoHeaderCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBMyListCell bundle:nil] forCellReuseIdentifier:kPBMyListCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBLoginOutCell bundle:nil] forCellReuseIdentifier:kPBLoginOutCell];
}
- (void)configureData {
    NSDictionary *param = @{};
    [TMNetwork requestGetWithParam:param url:@"/user/getUserInfo" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            self.model = [PBMyInfoModel objectWithKeyValues:model.data];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 2) {
        return 2;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        PBMyInfoHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBMyInfoHeaderCell forIndexPath:indexPath];
        [cell refreshWithModel:self.model];
        return cell;
    }else if (indexPath.section == 3){
        PBLoginOutCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBLoginOutCell forIndexPath:indexPath];
        cell.jumpVC = self;
        return cell;
    }else {
        PBMyListCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBMyListCell forIndexPath:indexPath];
        [cell refreshMyInfoWithModel:self.model indexPath:indexPath];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [self changeHeaderImage];
        }else {
            [self changeNickName];
        }
    }else if (indexPath.section == 1) {
        if (self.model.identityState == 0 || self.model.identityState == 3) {
            [self enterVerifyVC];
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            [self enterAboutMeVC];
        }else {
            [self enterSuggestVC];
        }
    }
}

- (void)changeHeaderImage {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择类型" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openImage:1];
    }];
    UIAlertAction *lifeAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openImage:0];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:okAction];
    [alertController addAction:lifeAction];
    [alertController addAction:cancelAction];
    // 出现
    [self presentViewController:alertController animated:YES completion:^{
        NSLog(@"presented");
    }];
}

- (void)changeNickName {
    PBChangeNickNameController *vc = [[PBChangeNickNameController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.nickName = self.model.nickname;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)enterSuggestVC {
    // 投诉建议
    PBSuggestController *vc = [[PBSuggestController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)enterVerifyVC {
    PBVerifyInfoController *vc = [[PBVerifyInfoController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)enterAboutMeVC {
    PBAboutMeController *vc = [[PBAboutMeController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - image
- (void)openImage:(NSInteger)type
{
    UIImagePickerController *picker =[[UIImagePickerController alloc]init];
    picker.allowsEditing= YES;
    picker.delegate= self;
    picker.sourceType = type;
    [self.navigationController presentViewController:picker animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imageData = UIImagePNGRepresentation(image);
    [self showHud];
    [TMNetwork requestImageWithImage:imageData url:@"/user/uploadHeadImg" callback:^(TMBaseReqModel *model) {
        [self hideHud];
        [picker dismissViewControllerAnimated:YES completion:nil];
        if (model.code == 200) {
            [self showHint:@"头像修改成功"];
        }else {
            [self showHint:model.message];
        }
    }];
}

#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    self.title = @"个人设置";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
